/**
 * ------------------------------------------------------------------
 *Copyright reserved by Emin Technology
 *---- --------------------------------------------------------------

 *PROJECT:		Base
 *FILE:			BaseUndeleteableServiceImpl.java
 *AUTHOR:		sunny
 *DATE CREATION:2011-12-05
 *DESCRIPTION:	Base service about status

 *==========================================================
 * VERSION CONTROL
 *Number – Date – Author – SVN – Description
 *01 –  2011-12-05 – sunny – xxx – Base service about status
 *-------------------------------------------------------------------------------------------------------
 */
package top.jle.jpa.basic.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import top.jle.jpa.basic.dao.*;
import top.jle.jpa.basic.entities.UnDeletableEntity;
import top.jle.jpa.basic.exception.BasicCustomException;
import top.jle.jpa.basic.exception.BasicExceptionCode;

import java.util.List;

public class UndeleteableServiceImpl<T extends UnDeletableEntity> extends CRUDServiceImpl<T> implements UndeleteableService<T> {

	/** 日志 */
	private static final Logger LOGGER = LoggerFactory.getLogger(UndeleteableServiceImpl.class);

	@Override
	public void save(T entity) {
		if(entity.getStatus() != T.STATUS_INVALID){
			entity.setStatus(T.STATUS_VALID);
		}
		super.save(entity);
	}

	@Override
	public void saveOrUpdate(T entity) {

		if(entity.getStatus() != T.STATUS_INVALID){
			entity.setStatus(T.STATUS_VALID);
		}

		super.saveOrUpdate(entity);
	}



	@Override
	public void update(T entity) {
		if(entity.getStatus() != T.STATUS_INVALID){
			entity.setStatus(T.STATUS_VALID);
		}
		super.update(entity);
	}

	@Override
	public List<T> findAllByStatus(int status, SortKey... sortKeys) {
		PreFilter propertyFilter = PreFilters.eq(T.PROP_STATUS,status);
		return this.getEntityDao().findByPreFilter(clazz,sortKeys, propertyFilter);
	}


	@Override
	public  int getCountByStatus(int status) {
		PreFilter statusFilter = PreFilters.eq(T.PROP_STATUS, status);
		return this.getEntityDao().getCountByPreFilter(clazz, statusFilter);
	}

	@Override
	public PagedResult<T> getPageByStatus(PageRequest pageRequest, int status)  {
		PreFilter statusFilter = PreFilters.eq(T.PROP_STATUS, status);
		return this.getEntityDao().getPage(clazz,pageRequest,statusFilter);
	}


	@Override
	public  void disable(T entity)  {
		LOGGER.debug("Disable the entity");
		entity.setLastModifyTime(System.currentTimeMillis());
		entity.setStatus(T.STATUS_INVALID);
		super.update(entity);
	}
	@Override
	@Transactional
	public void disable(long entityId){
		T t = findById(entityId);
		if(t != null){
			disable(t);
		}
	}


	@Override
	@Transactional
	public void deleteById(Long entityId) {
		if(entityId == null){
			throw new BasicCustomException(BasicExceptionCode.INVALID_PARAM);
		}
		this.disable(entityId);
	}

	@Override
	@Transactional
	public void delete(T entity) {
		LOGGER.debug("Disable the entity.");
		this.disable(entity);
	}

	@Override
	@Transactional
	public void deleteByIds(Long[] ids) {

		List<T> list = this.findByIds(ids);
		for(T t : list){
			this.disable(t);
		}
	}


	@Override
	public void enableById(Long id) {

		T entity = this.findById(id);
		if(entity == null){
			throw new BasicCustomException(BasicExceptionCode.DATA_NOT_EXISTS);
		}
		this.enable(entity);

	}

	@Override
	public void enable(T entity) {
		Long id = entity.getId();
		if(id == null){
			throw new BasicCustomException(BasicExceptionCode.INTERFACE_ERROR);
		}
		entity.setStatus(T.STATUS_VALID);
		this.update(entity);
	}

	@Override
	public void enableByIds(Long[] ids) {
		List<T> list = this.findByIds(ids);
		for(T t : list){
			this.enable(t);
		}
	}

	@Override
	public PreFilter getStatusFilter(){
		return PreFilters.ge(UnDeletableEntity.PROP_STATUS, UnDeletableEntity.STATUS_VALID);
	}

}
